package org.tedu.weibo.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.tedu.weibo.common.result.JsonResult;
import org.tedu.weibo.mapper.UserMapper;
import org.tedu.weibo.mapper.WeiboMapper;
import org.tedu.weibo.pojo.dto.UserRegDTO;
import org.tedu.weibo.pojo.dto.WeiboDTO;
import org.tedu.weibo.pojo.entity.User;
import org.tedu.weibo.pojo.entity.Weibo;
import org.tedu.weibo.pojo.vo.WeiboDetailVO;
import org.tedu.weibo.pojo.vo.WeiboIndexVO;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

@Api(tags = "微博模块")
@RestController
@RequestMapping("/v1/weibo")
@Validated
public class WeiboController {
    @Autowired
    private WeiboMapper weiboMapper;

    @Autowired
    private RedisTemplate<String, Serializable> template;

    @ApiOperation("添加微博")
    @PostMapping("/insert")
    public int insert(@RequestBody WeiboDTO weiboDTO, @ApiIgnore HttpSession session) {
        User user = (User)session.getAttribute("user");
        if(user == null){
            return 2;
        }
        Weibo weibo = new Weibo();
        BeanUtils.copyProperties(weiboDTO, weibo);
        weibo.setCreated(new Date());
        weibo.setUserId(user.getId());
        int num = weiboMapper.insert(weibo);
        return num;
    }

    @ApiOperation("查询所有微博")
    @GetMapping("/selectIndex")
    public JsonResult selectIndex() throws JsonProcessingException {
        ValueOperations<String, Serializable> ops = template.opsForValue();
        ObjectMapper objectMapper = new ObjectMapper();

        String weiboList = (String)ops.get("weiboList");
        if (weiboList == null) {
            List<WeiboIndexVO> list = weiboMapper.selectIndex();
            String json = objectMapper.writeValueAsString(list);
            ops.set("weiboList", json);
            return JsonResult.success(list);
        }
        else {
            List<WeiboIndexVO> list = objectMapper.readValue(weiboList, List.class);
            return JsonResult.success(list);
        }
    }

    @ApiOperation("通过id查找微博")
    @ApiImplicitParam(name = "id", value = "微博ID", required = true, dataType = "int", example = "1", paramType = "form")
    @GetMapping("/selectById")
    public JsonResult selectById(@Range(min=1,max=9999,message="微博ID必须在1-9999之间") int id) {
        return JsonResult.success(weiboMapper.selectById(id));
    }

    @ApiOperation("通过id和name查找微博")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "微博ID", required = true, dataType = "int", example = "1"),
            @ApiImplicitParam(name = "name", value = "用户名", required = true, dataType = "String", example = "Lu")
    })
    @GetMapping("/selectByIdAndName")
    public WeiboDetailVO selectWeiboDetailByIdAndName(int id, String name) {
        return weiboMapper.selectWeiboById(id, name);
    }
}
